package com.growthsee.rider.utils.ci;

import com.growthsee.rider.domain.view.RiderAttachmentView;
import com.growthsee.rider.utils.BasicPathUtil;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.UploadResult;
import com.qcloud.cos.transfer.Transfer;
import com.qcloud.cos.transfer.TransferProgress;
import com.qcloud.cos.transfer.Upload;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TransferManager {
    public static RiderAttachmentView uploadFile(MultipartFile multipartFile) {
        COSClient cosclient=null;
        com.qcloud.cos.transfer.TransferManager transferManager=null;
        try {
            File localFile = File.createTempFile("temp",null);   //生成临时文件
            multipartFile.transferTo(localFile);   //将MultipartFile类型转为File类型
             cosclient = QCloudCosUtils.getCOSClient();
            String bucketName = BasicPathUtil.getBucketName();
            ExecutorService threadPool = Executors.newFixedThreadPool(32);
            transferManager = new com.qcloud.cos.transfer.TransferManager(cosclient, threadPool);
            String newFileName = QCloudCosUtils.generateUniqueName(localFile.getName());  //生成唯一文件名
            String key = BasicPathUtil.getPrefix()+newFileName; //文件在存储桶中的key
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
            Upload upload = transferManager.upload(putObjectRequest);
            showTransferProgress(upload);
            UploadResult uploadResult = upload.waitForUploadResult();

            //数据入库
            String fileName = multipartFile.getOriginalFilename();
            RiderAttachmentView riderAttachmentView = new RiderAttachmentView();
            riderAttachmentView.setName(fileName.substring(0,fileName.lastIndexOf(".")));
            riderAttachmentView.setSize(multipartFile.getSize());
            riderAttachmentView.setKeyValue(key);
            riderAttachmentView.setContentType(multipartFile.getContentType());
            riderAttachmentView.setSuffix(fileName.substring(fileName.lastIndexOf(".") + 1));
            riderAttachmentView.setUrl(BasicPathUtil.getCloudurl()+key);
            return riderAttachmentView;

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(!Objects.isNull(cosclient))  cosclient.shutdown();
            if(!Objects.isNull(transferManager)) transferManager.shutdownNow();
        }
        return null;
    }

    private static void showTransferProgress(Transfer transfer) {
        System.out.println(transfer.getDescription());
        do {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                return;
            }
            TransferProgress progress = transfer.getProgress();
            long so_far = progress.getBytesTransferred();
            long total = progress.getTotalBytesToTransfer();
            double pct = progress.getPercentTransferred();
            System.out.printf("[%d / %d]\n", so_far, total);
        } while (transfer.isDone() == false);
        System.out.println(transfer.getState());
    }
}
